//
//  CommunityItemCell.swift
//  NDAi
//
//  Created by 丸子 on 2024/8/19.
//

import UIKit

class CommunityItemCell: UICollectionViewCell {
    
    private lazy var lookBtn:  UIButton = {
        let button = UIButton(type: .custom)
        button.setImage(UIImage(named: "icon_com_gz"), for: .normal)
        button.titleFont = UIFont.pingFangSCRegular(ofSize: 12)
        button.titleColorForNormal = .white
        button.layoutButtonWithEdgeInsetsStyle(.Left, space: 4)
        button.contentEdgeInsets = UIEdgeInsets(top: 0, left: 8, bottom: 0, right: 8)
        button.backgroundColor = UIColor(hexString: "#000000", transparency: 0.5)
        button.allRound(8)
        button.setTitle("0", for: .normal)
        return button
    }()
    
    private lazy var playerIV: UIImageView = {
        let imageView = UIImageView()
        imageView.allRound(4)
        imageView.contentMode = .scaleAspectFill
        imageView.layer.masksToBounds = true
        imageView.clipsToBounds = true
        imageView.image = UIImage(named: "icon_com_player")
        return imageView
    }()
    
    private lazy var iconIV: UIImageView = {
        let imageView = UIImageView()
        imageView.allRound(4)
        imageView.contentMode = .scaleAspectFill
        imageView.layer.masksToBounds = true
        imageView.clipsToBounds = true
        imageView.backgroundColor = UIColor(hexString: "#25262B")
        
        return imageView
    }()
    
    lazy var titleLb: UILabel = {
        let label = UILabel()
        label.textColor = Theme.color.text_d1
        label.font = UIFont.pingFangSCRegular(ofSize: 12)
        label.textAlignment = .center
        label.text = "唯美"
        label.textAlignment = .left
        return label
    }()
    
    private lazy var avatarIV: UIImageView = {
        let imageView = UIImageView()
        imageView.allRound(12)
        imageView.contentMode = .scaleAspectFill
        imageView.layer.masksToBounds = true
        imageView.clipsToBounds = true
        imageView.image = UIImage(named: "默认头像-页头")
        return imageView
    }()
    
    lazy var nickNameLb: UILabel = {
        let label = UILabel()
        label.textColor = Theme.color.text_d3
        label.font = UIFont.pingFangSCRegular(ofSize: 10)
        label.textAlignment = .center
        label.text = "用户-0"
        label.textAlignment = .left
        return label
    }()
    
    lazy var likeBtn:UIButton = {
        let button = UIButton(type: .custom)
        button.setImage(UIImage(named: "icon_com_likeNor"), for: .normal)
        button.setImage(UIImage(named: "icon_com_likeSel"), for: .selected)
        return button
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .clear
        contentView.addSubviews([iconIV, titleLb, avatarIV, nickNameLb, likeBtn, playerIV, lookBtn])
        contentView.backgroundColor = .white
        contentView.allRound(8)
        iconIV.snp.makeConstraints { make in
            make.top.leading.trailing.equalToSuperview()
            make.bottom.equalTo(-68)
        }
        
        lookBtn.snp.makeConstraints { make in
            make.leading.top.equalToSuperview().offset(8)
            make.height.equalTo(16)
        }
        
        titleLb.snp.makeConstraints { make in
            make.top.equalTo(iconIV.snp.bottom).offset(4)
            make.leading.trailing.equalToSuperview().offset(6)
            make.height.equalTo(20)
        }
        
        avatarIV.snp.makeConstraints { make in
            make.top.equalTo(titleLb.snp.bottom).offset(8)
            make.leading.equalToSuperview().offset(6)
            make.width.height.equalTo(24)
        }
        
        nickNameLb.snp.makeConstraints { make in
            make.centerY.equalTo(avatarIV)
            make.leading.equalTo(avatarIV.snp.trailing).offset(5)
            make.trailing.equalTo(likeBtn.snp.leading).offset(-8)
        }
        
        likeBtn.snp.makeConstraints { make in
            make.centerY.equalTo(avatarIV)
            make.trailing.equalTo(-8)
        }
        
        playerIV.snp.makeConstraints { make in
            make.centerX.centerY.equalTo(iconIV)
        }
    }
    
    func setup(_ model:CommunityModel) {
        iconIV.sd_setImage(with: URL(string: model.img_url ?? ""))
//        self.setTitleStyle(content: "视频唯美", style: "视频")
    
        if model.type == 1 {
            if let content = model.data_desc?.replacingOccurrences(of: "\n", with: "") {
                self.setTitleStyle(content: content, type: model.type ?? 1)
            }
        }else {
            if let content = model.title?.replacingOccurrences(of: "\n", with: "") {
                self.setTitleStyle(content: content, type: model.type ?? 1)
            }
        }
        
        avatarIV.sd_setImage(with: URL(string: model.icon), placeholderImage: UIImage(named: "默认头像-页头"))
        nickNameLb.text = model.nickname ?? ""
        lookBtn.titleForNormal = model.view_count?.formatted
        likeBtn.isSelected = model.is_like == 1 ? true : false
        playerIV.isHidden = model.type == 1 ? true : false
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setTitleStyle(content:String, type:Int) {
        // 创建一个 NSMutableAttributedString
        let attributedString = NSMutableAttributedString(string: " " + content)
        // 创建一个 NSTextAttachment 并设置图片
        let imageAttachment = NSTextAttachment()
        imageAttachment.bounds = CGRect(x: 0, y: -3, width: 36, height: 16) // 调整图片大小和位置
        if type == 1 {
            imageAttachment.image = UIImage(named: "icon_com_dark")
        }else if type == 2 {
            imageAttachment.image = UIImage(named: "icon_com_video")
        }else if type == 3 {
            imageAttachment.image = UIImage(named: "icon_com_music")
        }else {
            imageAttachment.image = UIImage(named: "icon_com_music")
        }
        // 创建一个带有图片的 NSAttributedString
        let imageString = NSAttributedString(attachment: imageAttachment)
        // 将图片插入到第一个字符的位置
        attributedString.insert(imageString, at: 0)
        // 设置 UILabel 的 attributedText
        titleLb.attributedText = attributedString
    }
    
}
